package com.rmy.queue.service.impl;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson2.JSONObject;
import com.rmy.common.core.text.Convert;
import com.rmy.common.utils.DateUtils;
import com.rmy.framework.aspectj.RateLimiterAspect;
import com.rmy.framework.manager.AsyncManager;
import com.rmy.queue.common.ErrorTipsConst;
import com.rmy.queue.common.QueueConst;
import com.rmy.queue.common.factory.DeviceAsyncFactory;
import com.rmy.queue.common.webSocket.CompShowWebSocketServer;
import com.rmy.queue.domain.QmsMainCalledResp;
import com.rmy.queue.domain.QmsQueueList;
import com.rmy.queue.domain.QmsWindowBerr;
import com.rmy.queue.service.IQmsWindowBerrService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.rmy.queue.mapper.QmsBerrConfigMapper;
import com.rmy.queue.domain.QmsBerrConfig;
import com.rmy.queue.service.IQmsBerrConfigService;
import org.springframework.util.Assert;

/**
 * 鹤位屏配置Service业务层处理
 *
 * @author rmy
 * @date 2022-10-06
 */
@Service
public class QmsBerrConfigServiceImpl implements IQmsBerrConfigService
{
    private static final Logger logger = LoggerFactory.getLogger(IQmsBerrConfigService.class);

    @Autowired
    private QmsBerrConfigMapper qmsBerrConfigMapper;

    @Autowired
    private IQmsWindowBerrService qmsWindowBerrService;

    /**
     * 查询鹤位屏配置
     *
     * @param id 鹤位屏配置主键
     * @return 鹤位屏配置
     */
    @Override
    public QmsBerrConfig selectQmsBerrConfigById(Long id)
    {
        return qmsBerrConfigMapper.selectQmsBerrConfigById(id);
    }

    /**
     * 查询鹤位屏配置列表
     *
     * @param qmsBerrConfig 鹤位屏配置
     * @return 鹤位屏配置
     */
    @Override
    public List<QmsBerrConfig> selectQmsBerrConfigList(QmsBerrConfig qmsBerrConfig)
    {
        return qmsBerrConfigMapper.selectQmsBerrConfigList(qmsBerrConfig);
    }

    /**
     * 新增鹤位屏配置
     *
     * @param qmsBerrConfig 鹤位屏配置
     * @return 结果
     */
    @Override
    public int insertQmsBerrConfig(QmsBerrConfig qmsBerrConfig)
    {
        qmsBerrConfig.setCreateTime(DateUtils.getNowDate());
        return qmsBerrConfigMapper.insertQmsBerrConfig(qmsBerrConfig);
    }

    /**
     * 修改鹤位屏配置
     *
     * @param qmsBerrConfig 鹤位屏配置
     * @return 结果
     */
    @Override
    public int updateQmsBerrConfig(QmsBerrConfig qmsBerrConfig)
    {
        qmsBerrConfig.setUpdateTime(DateUtils.getNowDate());
        return qmsBerrConfigMapper.updateQmsBerrConfig(qmsBerrConfig);
    }

    /**
     * 批量删除鹤位屏配置
     *
     * @param ids 需要删除的鹤位屏配置主键
     * @return 结果
     */
    @Override
    public int deleteQmsBerrConfigByIds(Long[] ids) throws Exception {
        for (Long id : ids) {
            QmsWindowBerr qmsWindowBerr = qmsWindowBerrService.selectQmsWindowBerrByBerrId(id);
            if (null != qmsWindowBerr) {
                throw new  Exception("存在绑定的窗口，不允许删除");
            }
        }
        return qmsBerrConfigMapper.deleteQmsBerrConfigByIds(ids);
    }

    /**
     * 删除鹤位屏配置信息
     *
     * @param id 鹤位屏配置主键
     * @return 结果
     */
    @Override
    public int deleteQmsBerrConfigById(Long id)
    {
        return qmsBerrConfigMapper.deleteQmsBerrConfigById(id);
    }

    @Override
    public List<QmsBerrConfig> selectBindedBerrList(QmsBerrConfig qmsBerrConfig) {
        return qmsBerrConfigMapper.selectBindedBerrList(qmsBerrConfig);
    }

    @Override
    public List<QmsBerrConfig> selectUnbindedBerrList(QmsBerrConfig qmsBerrConfig) {
        return qmsBerrConfigMapper.selectUnbindedBerrList(qmsBerrConfig);
    }

    @Override
    public boolean sendBerr(String orgNo, Long windowId, Map<String, Object> mapParams) throws IOException {
        QmsBerrConfig qmsBerrConfig = new QmsBerrConfig();
        qmsBerrConfig.setWindowId(windowId);
        qmsBerrConfig.setOrgNo(orgNo);
        List<QmsBerrConfig> qmsBerrConfigList = this.selectBindedBerrList(qmsBerrConfig);
        if (qmsBerrConfigList == null || qmsBerrConfigList.size() == 0) {
            logger.warn("sendBerr:"+ErrorTipsConst.TIPS_WINDOW_NOT_FOUND);
        }
//        Assert.notEmpty(qmsBerrConfigList, ErrorTipsConst.TIPS_WINDOW_NOT_FOUND);
        for (QmsBerrConfig tmpBerr : qmsBerrConfigList) {
            if ("1".equals(tmpBerr.getBerrType())) {
                String strContent = Convert.toStr(tmpBerr.getContentTemplate(), "");
                int z = 1;

                for (Map.Entry<String, Object> entry : mapParams.entrySet()) {
                    strContent = strContent.replace(entry.getKey(), Convert.toStr(entry.getValue(), ""));
                }

                // 异步处理
                AsyncManager.me().execute(
                        DeviceAsyncFactory.sendBerr(tmpBerr.getDeviceIp(),
                                tmpBerr.getDevicePort().intValue(),
                                tmpBerr.getJsonSettings(), strContent));
            } else {
                // 电视屏推送 通过websocket
//                JSONObject jsonObject = new JSONObject();
//                jsonObject.put("cmd", QueueConst.WEBSOCKET_CMD.CMD_CALL_NUMBER_NOTICE);
//                CompShowWebSocketServer.sendMessage(tmpBerr.getOrgNo(), jsonObject.toString());
            }
        }
        return true;
    }
}
